package com.skynet.supervision.utils;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

public class DocumentUtils {

    @SneakyThrows
    public static Document getDocument(MultipartFile file){
        InputStream is = file.getInputStream();
        Document docx = new Document();
        String last = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        if(last.equals("docx")){
            docx.loadFromStream(is, FileFormat.Docx);
        }
        else if(last.equals("doc")){
            docx.loadFromStream(is, FileFormat.Doc);
        }
        is.close();
        return docx;
    }
}
